<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:include value="/comm/header.jsp" />
<s:include value="/comm/menu.jsp" />
<!--第1部分开始  -->
<div class="wrap1">
	<div class="blank1"></div>
	<div class="outbox">
		<!--选项卡开始-->
		<s:form id="form1" name="editCanBuy" action="editCanBuy" method="post"
			enctype="multipart/form-data">
			<s:hidden name="canBuyModel.canId" />
			<div id="mainTabbox7">
				<div class="tab7_tt">修改代购信息</div>
				<div id="tabbox7" class="tabbox7_box">
					<ul>
						<li id="taba1" class="hover"><a>基本信息</a></li>
						<li id="taba2"><a>物品详情</a></li>
						<li id="taba3"><a>运输与服务</a></li>
					</ul>
				</div>
				<div id="conten">
					<div class="tab_wrap7">
						<div>其中加*的为必填项,其他为选填项</div>
						<div class="tab7_show" id="con_taba_1">
							<div class="input_box_addCanBuy">
								<!--选项卡1内容开始-->
								<div class="wrap_left" style="width: 50%">
									<!-- -->
									<div class="input_box">
										<ul>
											<li class="t1"><span class="span33">名称</span></li>
											<li class="t2"><s:textfield name="canBuyModel.goodsName" id="goodsName" cssClass="input1" maxlength="35"/>
												<label style="color:red">&nbsp;*</label><br />
												<label id="goodsNamePrompt" style="display: none; color: red">物品名称不能为空</label></li>
											<li class="t1"><span class="span33">类别</span></li>
											<li class="t2"><s:select name="canBuyModel.categoryId"
													list="categorys" listKey="categoryId"
													listValue="categoryName"/></li>
											<li class="t1"><span class="span33">所属区域</span></li>
											<li class="t2"><s:select name="canBuyModel.areaId"
													list="areas" listKey="areaId" listValue="areaName" /></li>
											<li class="t1"><span class="span33">价格</span></li>
											<li class="t2"><s:textfield name="canBuyModel.price" id="price" cssClass="input1" />
												<label style="color:red">&nbsp;*</label><br />
												<label id="pricePrompt" style="display: none; color: red">请输入正确的价格</label>
											</li>
											<li class="t1"><span class="span33">佣金比例</span></li>
											<li class="t2">
												<s:textfield name="canBuyModel.ratio" id="ratio" cssClass="input1" />
												<label style="color:red">&nbsp;*</label>%<br /> 
												<label id="ratioPrompt" style="display: none; color: red"></label></li>
											<li class="t1"><span class="span33">佣金</span></li>
											<li class="t2"><s:textfield id="serviceFee" name="canBuyModel.serviceFee" cssClass="input1"  readonly="true" /></li>
											<li class="t1"><span class="span33">国内价</span></li>
											<li class="t2">
												<s:hidden name="canBuyModel.domesticPrice" id="domesticPriceId" />
												<s:if test="canBuyModel.domesticPrice <= 0">
													<input type="text" id="domesticPrice" name="domesticPrice" class="input1" value="国内无此产品" /><br/>
												</s:if>
												<s:else>
													<input type="text" id="domesticPrice" name="domesticPrice" class="input1" value="<s:property value='canBuyModel.domesticPrice' />" /><br/>	
												</s:else>
												<label id="domesticPricePrompt" style="color: red"></label>
											</li>
											<li class="t1"><span class="span33">来源</span></li>
											<li class="t2"><s:select name="canBuyModel.source"
													list='#{"1":"大型超市","2":"便利店","3":"专卖店","4":"生产厂家","5":"其他" }'
													theme="simple"/></li>
											<li class="t1"><span class="span33">单据类型</span></li>
											<li class="t2"><s:select name="canBuyModel.receipts"
													list='#{"1":"小票","2":"发票","3":"发票或小票复印件" }' theme="simple"/></li>

										</ul>

									</div>
									<div class="clear"></div>
									<!-- -->
								</div>
								<div class="wrap_left">
									<!-- -->
									<div class="input_box">
										<ul>
											<li class="t1"><span class="span33">首件运费</span></li>
											<li class="t2">
												<s:textfield id="shippingCost" name="canBuyModel.logistics.price" cssClass="input1" />
												<br/>
												<label id="shippingCostPrompt" style="color: red"></label>
											</li>
											<li class="t1"><span class="span33">续件运费</span></li>
											<li class="t2">
												<s:textfield id="continuedPrice" name="canBuyModel.logistics.continuedPrice" cssClass="input1" />
												<br/>
												<label id="continuedPricePrompt" style="color: red"></label>
											</li>

											<li class="t1"><span class="span33">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;描述</span></li>
											<li class="t2">
												<s:textfield name="canBuyModel.description" cssClass="input1" maxlength="23"/>
											</li>
											<li class="t1"><span class="span33">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;图片</span></li>
											<li class="t2">
												<label id="imagePrompt" style="line-height:30px;color:red;"></label><br/>
												<ul>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
													<li><s:file name="image" cssClass="input5"></s:file></li>
												</ul>
											</li>
											
										</ul>

									</div>
									<div class="clear"></div>
									<!-- -->
								</div>

								<div class="clear"></div>
								


								<!--选项卡1内容结束-->
							</div>

						</div>
						<div class="tab7_show" id="con_taba_2">
							<s:textarea name="canBuyModel.details" cols="160" rows="10"  cssClass="xheditor"></s:textarea>
						</div>
						<div class="tab7_show" id="con_taba_3">
							<b>请填写配送信息、售后服务条款以及关税等注意事项</b><br/>
							<s:textarea name="canBuyModel.serviceAssurance" cols="160"
								rows="10"  cssClass="xheditor"></s:textarea>
						</div>

					</div>
				</div>
			</div>
		</s:form>
		<!--选项卡结束-->
		<div class="but_submit" style="text-align: center">
			<br />
			<a id="editCanBuy" href="javascript:editCanBuy();">确认无误，提交</a>
		</div>
	</div>

</div>
<s:include value="/comm/bottom.jsp" />
<script type="text/javascript">

var submit = false;
$("#goodsName").blur(validateGoodsName);
$("#price").blur(function(){if (validatePrice()) {calcServiceFee();}});
$("#ratio").blur(function() {if (validateRatio()) {calcServiceFee();}});
$("#domesticPrice").blur(validateDomesticPrice);
$("#shippingCost").blur(function(){validateShippingCost($(this).val(),"shippingCostPrompt");});
$("#continuedPrice").blur(function(){validateShippingCost($(this).val(),  "continuedPricePrompt");});
$("#serviceFee").blur(calcServiceFee);

$("#taba1").click(function(){setTab('taba',1,3);});
$("#taba2").click(function(){setTab('taba',2,3);});
$("#taba3").click(function(){setTab('taba',3,3);});

$("[name=image]").each(function() {
	$(this).change(function() {
		var image = $(this).val();
		if(!/\.(jpeg|png|JPEG|PNG)$/.test(image)) {
			$("#imagePrompt").html("图片类型必须是.jpeg,png中的一种");
        }
		else {
			$("#imagePrompt").html("");	
		}
	});
});

function editCanBuy() {

	if (!validateGoodsName()) {
		return;
	}
	if (!validatePrice()) {
		return;
	}
	if (!validateRatio()) {
		return;
	}
	if (!validateDomesticPrice()) {
		return;
	}
	if (!validateShippingCost()) {
		return;
	}
	
	if (!validateImage()) {
		return;
	}
	

	if (trim($("#shippingCost").val()) == "") {
		$("#shippingCost").val(0);
	}

	var domesticPrice = $("#domesticPrice").val();
	if (domesticPrice != "" && domesticPrice != "国内无此产品") {
		$("#domesticPriceId").val($("#domesticPrice").val());
	}

	if (!submit) {
		submit = true;
		$("#form1").submit();
	}
}

function validateImage() {
	var images = $("[name=image]");
	var valid = true;
	for (var i = 0; i < images.length; i++) {
		if (images[i].value != "") {
			if (!/\.(jpeg|png|JPEG|PNG)$/.test(images[i].value)) {
				valid = false;
				break;
			}
		}
	}
	if (!valid) {
		$("#imagePrompt").html("图片类型必须是.jpeg,png中的一种");
		return false;
	}
	return true;
}

function validateGoodsName() {
	var goodsName = trim($("#goodsName").val());
	if (goodsName == "") {
		$("#goodsNamePrompt").html("物品名称不能为空");
		$("#goodsNamePrompt").show();
		return false;
	}
	else {
		$("#goodsNamePrompt").hide();
		return true;
	}
}
	
function validatePrice() {
	var price = trim($("#price").val());
	if (price == "") {
		$("#pricePrompt").html("价格不能为空");
		$("#pricePrompt").show();
		return false;
	}
	else if (!priceRegex.test(price) || price <= 0) {
		$("#pricePrompt").html("请输入正确的价格");
		$("#pricePrompt").show();
		return false;
	} 
	else {
		$("#pricePrompt").hide();
		return true;
	}
}
	
function validateRatio() {
	var ratio = trim($("#ratio").val());
	if (ratio == "") {
		$("#ratioPrompt").html("佣金比例不能为空");
		$("#ratioPrompt").show();
		return false;
	}
	if (!digitRegex.test(ratio) || ratio <= 0) {
		$("#ratioPrompt").html("只能输入正整数");
		$("#ratioPrompt").show();
		return false;
	}
	$("#ratioPrompt").hide();
	return true;
}

//计算佣金
function calcServiceFee() {
	var price = $("#price").val();
	var ratio = $("#ratio").val();
	if (price != "" && priceRegex.test(price) && price > 0 && ratio != "" && digitRegex.test(ratio) && ratio > 0) {
		var serviceFee = price * ratio / 100;
		$("#serviceFee").val(serviceFee.toFixed(2));	
	}
}

function validateDomesticPrice() {
	var price = trim($("#domesticPrice").val());
	if (price == "" || price == "国内无此产品") {
		return true;
	}
	if (!priceRegex.test(price) || price <= 0) {
		$("#domesticPricePrompt").html("请输入正确的价格");
		$("#domesticPricePrompt").show();
		return false;
	}
	$("#domesticPricePrompt").hide();
	return true;
	
}

function validateShippingCost(price, pricePromptId) {
	if (arguments.length == 0) {
		return (validateShippingCost($("#shippingCost").val(), "shippingCostPrompt") && validateShippingCost($("#continuedPrice").val(), "continuedPricePrompt")); 
	}
	else {
		if (price != "" && (!priceRegex.test(price) || price < 0)) {
			$("#" + pricePromptId).html("请输入正确的运费");
			return false;
		}
		$("#" + pricePromptId).html("");
		return true;
	}	
}



	
</script>